package com.longtop.framework.sys.login.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.base.BaseService;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.log.service.LogService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.SpringBeanFactory;
import com.longtop.framework.util.StringUtil;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Text;

/**
 * 首页选择--全行贷款趋势图
 * 
 * @author 胡砥峰
 * @version 1.0
 */
public class Home11Action extends BaseAction {

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		LogService logService = SpringBeanFactory.getBean(LogService.BEAN_ID);
		BaseService baseService = SpringBeanFactory.getBean(BaseService.BEAN_ID); // 调用spring的业务逻辑
		SessionObject sessionObject = this.getSessionObject(context);

		String chartType = StringUtil.getString(context.getRequest().getParameter("chartType")); // 图形类型
		Text text = (Text) context.getControl("chartType");
		text.setValue(chartType);

		try {
			Dataset dataset = context.getDataset("Dataset_Home");
			HashMap hashMap = new HashMap();
			String filter = "";
			filter = sessionObject.getGrantFilterSql("T_SYS_ORG", "O", "ORG_ID", null);
			hashMap.put("grantFilterSql", filter);
			List list = baseService.queryForListByIbatis("homeChartSql.searchHomeChartPie", hashMap);
			List lastList = new ArrayList(5);
			String flag = "";
			for (int i = 0; i < list.size(); i++) {
				Map map = (Map) list.get(i);
				if (map.get("LABEL").equals("正常")) {
					flag = flag + "1";
				} else if (map.get("LABEL").equals("关注")) {
					flag = flag + "2";
				} else if (map.get("LABEL").equals("次级")) {
					flag = flag + "3";
				} else if (map.get("LABEL").equals("可疑")) {
					flag = flag + "4";
				} else if (map.get("LABEL").equals("损失")) {
					flag = flag + "5";
				}
			}
			if (flag.indexOf("1") < 0) {
				Map map = new HashMap();
				map.put("LABEL", "正常");
				map.put("VALUE", "0");
				list.add(map);
			}
			if (flag.indexOf("2") < 0) {
				Map map = new HashMap();
				map.put("LABEL", "关注");
				map.put("VALUE", "0");
				list.add(map);
			}
			if (flag.indexOf("3") < 0) {
				Map map = new HashMap();
				map.put("LABEL", "次级");
				map.put("VALUE", "0");
				list.add(map);
			}
			if (flag.indexOf("4") < 0) {
				Map map = new HashMap();
				map.put("LABEL", "可疑");
				map.put("VALUE", "0");
				list.add(map);
			}
			if (flag.indexOf("5") < 0) {
				Map map = new HashMap();
				map.put("LABEL", "损失");
				map.put("VALUE", "0");
				list.add(map);
			}

			for (int i = 0; list.size() > 0; i++) {
				Map map = (Map) list.get(i);
				if (map.get("LABEL").equals("正常") && list.size() == 5) {
					lastList.add(map);
					list.remove(i);
					i = -1;
				} else if (map.get("LABEL").equals("关注") && list.size() == 4) {
					lastList.add(map);
					list.remove(i);
					i = -1;
				} else if (map.get("LABEL").equals("次级") && list.size() == 3) {
					lastList.add(map);
					list.remove(i);
					i = -1;
				} else if (map.get("LABEL").equals("可疑") && list.size() == 2) {
					lastList.add(map);
					list.remove(i);
					i = -1;
				} else if (map.get("LABEL").equals("损失") && list.size() == 1) {
					lastList.add(map);
					list.remove(i);
					i = -1;
				}
			}

			BeanUtil.listToDataset(lastList, dataset);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
